/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月26日
 */
package com.massyframework.beanskin.runtime.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.massyframework.beanskin.service.ExportServiceDescriptor;
import com.massyframework.beanskin.service.ExportServiceDescriptorBuilder;
import com.massyframework.beanskin.service.ServiceReference;
import com.massyframework.beanskin.util.version.Version;

/**
 * 简单的输出服务描述构建器
 * @author huangkh
 *
 */
public final class SimpleExportServiceDescriptorBuilder implements ExportServiceDescriptorBuilder {

	protected String beanName;
	protected List<Class<?>> exportTypes;
	protected Map<String, Object> props;
	
	/**
	 * 构造方法
	 */
	public SimpleExportServiceDescriptorBuilder() {
		this.props = new HashMap<>();
	}
	
	/**
	 * 构造方法
	 * @param beanName {@link String},对应容器中的bean名称
	 */
	public SimpleExportServiceDescriptorBuilder(String beanName) {
		this.props = new HashMap<>();
		this.beanName = beanName;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder addExportService(Class<?> exportType) {
		if (exportType != null) {
			if (this.exportTypes == null) {
				this.exportTypes = new ArrayList<>();
			}
			this.exportTypes.add(exportType);
		}
		return this;
	}
	
	
	@Override
	public SimpleExportServiceDescriptorBuilder addExportServices(Collection<Class<?>> exportTypes) {
		if (exportTypes != null) {
			if (this.exportTypes == null) {
				this.exportTypes = new ArrayList<>();
			}
			this.exportTypes.addAll(exportTypes);
		}
		return this;
	}
	
	
	@Override
	public SimpleExportServiceDescriptorBuilder addExportServices(Class<?>[] exportTypes) {
		if (exportTypes != null) {
			if (this.exportTypes == null) {
				this.exportTypes = new ArrayList<>();
			}
			for (Class<?> exportType: exportTypes) {
				this.exportTypes.add(exportType);
			}
		}
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder beanName(String value) {
		this.beanName = value;
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder name(String value) {
		if (value != null) {
			this.props.put(ServiceReference.SERVICE_NAME, value);
		}
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder description(String value) {
		if (value != null) {
			this.props.put(ServiceReference.SERVICE_DESCRIPTION, value);
		}
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder isFactory(boolean value) {
		this.props.put(ServiceReference.SERVICE_ISFACTORY, value);
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder version(Version value) {
		this.props.put(ServiceReference.SERVICE_VERSION, value);
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder topRanking() {
		this.props.put(ServiceReference.SERVICE_RANKING, ServiceReference.SERVICE_RANKING_MAXVALUE);;
		return this;
	}

	@Override
	public SimpleExportServiceDescriptorBuilder property(String name, Object value) {
		this.props.put(name, value);
		return this;
	}
	
	

	@Override
	public ExportServiceDescriptorBuilder properties(Map<String, Object> props) {
		this.props.putAll(props);
		return this;
	}

	@Override
	public ExportServiceDescriptor build() {
		return new SimpleExportServiceDescriptor(this);
	}
}
